//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
// 		VisualNovelToolkit		/_/_/_/_/_/_/_/_/_/.
// Copyright ©2013 - Sol-tribe.	/_/_/_/_/_/_/_/_/_/.
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.

using UnityEngine;
using UnityEditor;
using System.Collections;
using ViNoToolkit;

/// <summary>
/// Draw templates tab in ViNoToolbar.
/// </summary>
public class DrawTemplatesTab {
			
	static public void Draw(){
		GUILayout.BeginHorizontal();

			if( GUILayout.Button( new GUIContent( "Conversation Scene" , ViNoEditorResources.convTemplateIcon ) , GUILayout.Width(150f) , GUILayout.Height (60f) ) ){
				CreateConvScene();
			}

			if( GUILayout.Button( new GUIContent( "ADV" , ViNoEditorResources.convTemplateIcon ) , GUILayout.Width(150f) , GUILayout.Height (60f) ) ){
				CreateAdvScene();
			}
		
		GUILayout.EndHorizontal();
	}

	[ MenuItem( "GameObject/ViNo/Templates/Create a Conversation Scene" ) ]	
	static private void CreateConvScene( ){
		string sceneName = "A Conversation Scene";
		string folderName = "A_Conversation";
		CreateConvScene( sceneName , folderName );
	}

	[ MenuItem( "GameObject/ViNo/Templates/Create an Adv Scene" ) ]	
	static private void CreateAdvScene( ){
//       ScriptableWizard.DisplayWizard<ADVCreateWizard>("Create your ADV", "Create" );
		EditorWindow.GetWindow( typeof(ADVCreateWizard) );
	}

	static public void CreateConvScene( string sceneName , string folderName ){
		string path = "Assets/" + ViNoToolUtil.kAssetName + "/Templates/ConversationScene with SimpleUI.prefab";

		// Create scene from Templates.
		CreateConvScene( sceneName , folderName , path );

		// Create Sampel Scenario Object in hierarchy.
		CreateSampleScenarioObject( folderName );
	}

	static public void CreateAdvScene( string sceneName , string folderName ){
		string path = "Assets/" + ViNoToolUtil.kAssetName + "/Templates/ADV Scene.prefab";

		// Create Adv Scene from Templates folder.
		CreateConvScene( sceneName , folderName , path );
	}

	static public void CreateConvScene( string sceneName , string folderName ,  string templScenePath ){
		GameObject sceneRoot = CreateTemplScene( templScenePath );
		sceneRoot.name = sceneName;

		string scenarioName = folderName;
		if( ! System.IO.Directory.Exists( "Assets/" + scenarioName ) ) {
			AssetDatabase.CreateFolder( "Assets" , scenarioName );			
		}

		if( ! System.IO.Directory.Exists( "Assets/" + scenarioName + "/Resources" ) ) {
			AssetDatabase.CreateFolder( "Assets/" + scenarioName , "Resources" );
		}

		/// Create actor and Scene Library in Project view.
		CreateActorAndSceneLibraryInProject( scenarioName );
	}

	static public void CreateActorAndSceneLibraryInProject(  string folderName ){
		// Create an ActorLibrary and a SceneLibrary.
		string actorLibPrefabPath = "Assets/" + folderName + "/ActorLibrary.prefab";
		string sceneLibPrefabPath = "Assets/" + folderName + "/SceneLibrary.prefab";

		ViNoScenarioDataUtil.CreateActorLibrary( actorLibPrefabPath );
		ViNoScenarioDataUtil.CreateSceneLibrary( sceneLibPrefabPath );

		GameObject.DestroyImmediate( GameObject.Find( "ActorLibrary") );
		GameObject.DestroyImmediate( GameObject.Find( "SceneLibrary") );

		ViNoToolkit.SceneEvent sceneEvt = GameObject.FindObjectOfType( typeof( ViNoToolkit.SceneEvent ) ) as ViNoToolkit.SceneEvent;

		ViNoToolkit.ActorLibrary actorLib = AssetDatabase.LoadAssetAtPath( actorLibPrefabPath , typeof( ViNoToolkit.ActorLibrary) ) as ViNoToolkit.ActorLibrary;	
		ViNoToolkit.SceneLibrary sceneLib = AssetDatabase.LoadAssetAtPath( sceneLibPrefabPath , typeof( ViNoToolkit.SceneLibrary) ) as ViNoToolkit.SceneLibrary;		

		sceneEvt.actorLib = actorLib;
		sceneEvt.sceneLib = sceneLib;
	}

	static public void CreateSampleScenarioObject( string scenarioName ){
		// Create a Sample Scenario object.
		GameObject scenarioObj = ViNoToolUtil.CreateANewScenario( scenarioName , true );
		GameObject startObj = scenarioObj.transform.FindChild( "START" ).gameObject;		

		GameObject node0=  new GameObject( "0_Scene" );
		LoadSceneNode loadSceneNode = node0.AddComponent<LoadSceneNode>();
		loadSceneNode.sceneName = "Scene1";
		loadSceneNode.method = LoadSceneNode.Methods.DESTROY_AND_LOAD;
		loadSceneNode.withFadeIn = true;
		
		loadSceneNode.transform.parent = startObj.transform;

		DialogPartNode dlgNode = ViNoToolUtil.AddDialogPartNode( startObj.transform );
		dlgNode.name = "1_Dialog";

//		DialogPartData data0 = dlgNode.AddData( "" , ""  );	// EnterScene.
		DialogPartData data1 = dlgNode.AddData( "" , ""  );	// EnterActor.
		DialogPartData data2 = dlgNode.AddData( "Sachi" , "Hello. I am Sachi."  );	// .Dialogs
		DialogPartData data3 = dlgNode.AddData( "Maiko" , "Hi. Sachi."  );
		DialogPartData data4 = dlgNode.AddData( "Sachi" , "Bye Bye !"  );						
		DialogPartData data5 = dlgNode.AddData( "Maiko" , "Bye Bye !"  );						
		DialogPartData data6 = dlgNode.AddData( "" , ""  );	// ExitActors.
//		DialogPartData data7 = dlgNode.AddData( "" , ""  );	// ExitScene.
/*
		data0.actionID = DialogPartNodeActionType.EnterScene;
		data0.scene = new DialogPartData.SceneEntry();
		data0.scene.sceneName = "Scene1";
		data0.scene.withFade = true;
//*/
		data1.actionID = DialogPartNodeActionType.EnterActor;
		data1.enterActorEntries = new DialogPartData.ActorEntry[ 2 ];
		data1.enterActorEntries[0] = new DialogPartData.ActorEntry();
		data1.enterActorEntries[0].actorName = "Sachi";
		data1.enterActorEntries[1] = new DialogPartData.ActorEntry();
		data1.enterActorEntries[1].actorName = "Maiko";
		data1.enterActorEntries[0].position = ViNoToolkit.SceneEvent.ActorPosition.middle_right;
		data1.enterActorEntries[1].position = ViNoToolkit.SceneEvent.ActorPosition.middle_left;

		data2.isName = true;
		data3.isName = true;
		data4.isName = true;
		data5.isName = true;

		data6.actionID = DialogPartNodeActionType.ExitScene;
		data6.scene = new DialogPartData.SceneEntry();
		data6.scene.withFade = true;

		CreateFlagAndSaveInfo( scenarioName ,  scenarioObj );
	}

	static public void CreateFlagAndSaveInfo( string scenarioName , GameObject scenarioObj ){
		string scenarioFlagDataPath = "Assets/" + scenarioName + "/" + "Flags.asset";
		string scenarioSaveDataPath = "Assets/" + scenarioName + "/" + "SaveData.asset";	

		// Create Game Info.
		ViNoSaveInfo saveInfo = ScriptableObjectUtility.CreateScriptableObject( "ViNoSaveInfo" , scenarioSaveDataPath ) as ViNoSaveInfo;				
		FlagTable flagTable = ScriptableObjectUtility.CreateScriptableObject( "FlagTable" , scenarioFlagDataPath ) as FlagTable;

		ScenarioNode scenarioNode = scenarioObj.GetComponent<ScenarioNode>();		
		scenarioNode.m_PlayAtStart = true;
		scenarioNode.flagTable = flagTable;

		ScenarioCtrl scenarioCtrl = GameObject.FindObjectOfType( typeof(ScenarioCtrl) ) as ScenarioCtrl;
		scenarioCtrl.quickSaveSlot = saveInfo;
		scenarioCtrl.saveInfo = saveInfo;
	}

	/// <summary>
	/// Creates the templ scene.
	/// </summary>
	/// <returns>
	///  return the Scene Root Object.
	/// </returns>
	static private GameObject CreateTemplScene( string templPrefabPath ){
		GameObject obj = AssetDatabase.LoadAssetAtPath(
				templPrefabPath , typeof( GameObject) ) as GameObject;
		
		GameObject clone = GameObject.Instantiate( obj ) as GameObject;
		ViNoGOExtensions.StripGameObjectName( clone , "(Clone)" , "" );
		return clone;
	}	
}
